package cn.school.service;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import cn.school.dao.StudentDao;
import cn.school.entity.Page;
import cn.school.entity.Student;
import cn.school.util.MyBatisUtil;

public class StudentService {

	public Page<Student> paging(int pageNo, int pageSize, String name, String tele, Date enterDateStart,
			Date enterEnd) {
		SqlSession s = MyBatisUtil.get();
		try {
			StudentDao studentDao = s.getMapper(StudentDao.class);
			List<Student> list = studentDao.search(pageNo, pageSize, name, tele, enterDateStart, enterEnd);
			int total = studentDao.searchTotal(name, tele, enterDateStart, enterEnd);
			return new Page<Student>(total, list, pageNo);
		} finally {
			MyBatisUtil.close(s);
		}
	}

	public List<Student> search(int pageNo, int pageSize, String name, String tele, Date enterDateStart,
			Date enterEnd) {
		SqlSession s = MyBatisUtil.get();
		try {
			StudentDao studentDao = s.getMapper(StudentDao.class);
			return studentDao.search(pageNo, pageSize, name, tele, enterDateStart, enterEnd);
		} finally {
			MyBatisUtil.close(s);
		}
	}

	public int searchTotal(String name, String tele, Date enterDateStart, Date enterEnd) {
		SqlSession s = MyBatisUtil.get();
		try {
			StudentDao studentDao = s.getMapper(StudentDao.class);
			return studentDao.searchTotal(name, tele, enterDateStart, enterEnd);
		} finally {
			MyBatisUtil.close(s);
		}
	}

	public boolean del(long stuno) {
		SqlSession s = MyBatisUtil.get();
		try {
			StudentDao studentDao = s.getMapper(StudentDao.class);
			return studentDao.batchdel(new long[] { stuno }) > 0;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			MyBatisUtil.close(s);
		}
		return false;
	}

	public Student get(long stuno) {
		SqlSession s = MyBatisUtil.get();
		try {
			StudentDao studentDao = s.getMapper(StudentDao.class);
			return studentDao.get(stuno);
		} finally {
			MyBatisUtil.close(s);
		}
	}

	public int update(Student stu) {
		SqlSession s = MyBatisUtil.get();
		try {
			StudentDao studentDao = s.getMapper(StudentDao.class);
			return studentDao.update(stu);
		} finally {
			MyBatisUtil.close(s);
		}
	}

	public boolean checkTele(String tele) {
		SqlSession s = MyBatisUtil.get();
		try {
			StudentDao studentDao = s.getMapper(StudentDao.class);
			return studentDao.checkTele(tele) > 0;
		} finally {
			MyBatisUtil.close(s);
		}
	}
}
